Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 875)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.03686 13.03258 13.02836 13.02418 13.02005 13.01597 13.01193 13.00794
##   [9] 13.00399 13.00010 12.99624 12.99244 12.98868 12.98497 12.98130 12.97768
##  [17] 12.97411 12.97058 12.96710 12.96367 12.96028 12.95693 12.95364 12.95039
##  [25] 12.94718 12.94402 12.94091 12.93784 12.93482 12.93184 12.92891 12.92602
##  [33] 12.92318 12.92038 12.91763 12.91493 12.91227 12.90966 12.90709 12.90456
##  [41] 12.90209 12.89965 12.89726 12.89493 12.89267 12.89047 12.88835 12.88629
##  [49] 12.88429 12.88236 12.88050 12.87870 12.87696 12.87528 12.87367 12.87212
##  [57] 12.87063 12.86920 12.86782 12.86651 12.86525 12.86405 12.86291 12.86182
##  [65] 12.86079 12.85981 12.85889 12.85802 12.85720 12.85643 12.85571 12.85505
##  [73] 12.85443 12.85386 12.85334 12.85287 12.85244 12.85206 12.85173 12.85144
##  [81] 12.85120 12.85099 12.85083 12.85071 12.85064 12.85060 12.85061 12.85065
##  [89] 12.85073 12.85085 12.85101 12.85120 12.85145 12.85179 12.85222 12.85272
##  [97] 12.85331 12.85397 12.85471 12.85551 12.85639 12.85734 12.85835 12.85943
## [105] 12.86057 12.86176 12.86302 12.86432 12.86569 12.86710 12.86855 12.87006
## [113] 12.87161 12.87319 12.87482 12.87649 12.87819 12.87992 12.88168 12.88347
## [121] 12.88528 12.88712 12.88898 12.89086 12.89275 12.89466 12.89658 12.89851
## [129] 12.90045 12.90240 12.90434 12.90629 12.90824 12.91018 12.91212 12.91478
## [137] 12.91881 12.92410 12.93053 12.93799 12.94637 12.95555 12.96541 12.97586
## [145] 12.98676 12.99801 13.00949 13.02109 13.03270 13.04419 13.05547 13.06641
## [153] 13.07689 13.08682 13.09606 13.10451 13.11206 13.11858 13.12397 13.12812
## [161] 13.13090 13.13336 13.13657 13.14047 13.14501 13.15012 13.15575 13.16185
## [169] 13.16834 13.17518 13.18230 13.18965 13.19716 13.20479 13.21247 13.22014
## [177] 13.22775 13.23523 13.24253 13.24960 13.25637 13.26278 13.26877 13.27430
## [185] 13.27929 13.28369 13.28745 13.29050 13.29279 13.29425 13.29484 13.29448
## [193] 13.29313 13.29073 13.28721 13.28251 13.27646 13.26897 13.26015 13.25011
## [201] 13.23896 13.22680 13.21373 13.19988 13.18535 13.17024 13.15466 13.13872
## [209] 13.12253 13.10619 13.08982 13.07352 13.05740 13.04156 13.02612 13.01118
## [217] 12.99685 12.98324 12.97045 12.95698 12.94134 12.92372 12.90426 12.88316
## [225] 12.86056 12.83664 12.81156 12.78550 12.75862 12.73108 12.70307 12.67473
## [233] 12.64625 12.61779 12.58951 12.56159 12.53419 12.50749 12.48163 12.45681
## [241] 12.43317 12.41090 12.39015 12.37110 12.35391 12.33764 12.32126 12.30479
## [249] 12.28824 12.27165 12.25503 12.23842 12.22182 12.20528 12.18880 12.17241
## [257] 12.15614 12.14001 12.12404 12.10826 12.09268 12.07733 12.06224 12.04743
## [265] 12.03292 12.01873 12.00489 11.99142 11.97872 11.96710 11.95651 11.94685
## [273] 11.93805 11.93003 11.92273 11.91604 11.90991 11.90426 11.89900 11.89406
## [281] 11.88936 11.88482 11.88037 11.87593 11.87142 11.86676 11.86188 11.85670
## [289] 11.85114 11.84512 11.83858 11.83142 11.82357 11.81496 11.80561 11.79565
## [297] 11.78516 11.77420 11.76286 11.75121 11.73932 11.72727 11.71513 11.70298
## [305] 11.69089 11.67893 11.66719 11.65573 11.64463 11.63397 11.62381 11.61424
## [313] 11.60532 11.59714 11.58976 11.58327 11.57773 11.57206 11.56523 11.55736
## [321] 11.54856 11.53898 11.52872 11.51792 11.50671 11.49520 11.48352 11.47181
## [329] 11.46017 11.44875 11.43765 11.42702 11.41697 11.40763 11.39912 11.39157
## [337] 11.38511 11.37986 11.37594 11.37349 11.37261 11.37345 11.37613 11.38012
## [345] 11.38481 11.39016 11.39615 11.40275 11.40995 11.41770 11.42599 11.43479
## [353] 11.44407 11.45381 11.46397 11.47454 11.48548 11.49678 11.50840 11.52032
## [361] 11.53251 11.54494 11.55759 11.57043 11.58344 11.59659 11.61144 11.62943
## [369] 11.65030 11.67384 11.69980 11.72795 11.75806 11.78990 11.82322 11.85779
## [377] 11.89339 11.92977 11.96670 12.00394 12.04127 12.07845 12.11524 12.15141
## [385] 12.18672 12.22094 12.25384 12.28518 12.31473 12.34224 12.36750 12.39026
## [393] 12.41029 12.42736 12.44340 12.46044 12.47835 12.49704 12.51638 12.53626
## [401] 12.55657 12.57718 12.59800 12.61889 12.63976 12.66048 12.68094 12.70102
## [409] 12.72062 12.73961 12.75789 12.77534 12.79184 12.80729 12.82156 12.83454
## [417] 12.84612 12.85619 12.86463 12.87132 12.87614 12.87912 12.88040 12.88012
## [425] 12.87841 12.87542 12.87129 12.86615 12.86014 12.85340 12.84607 12.83828
## [433] 12.83018 12.82190 12.81359 12.80537 12.79740 12.78980 12.78272 12.77629
## [441] 12.77066 12.76596 12.76233 12.75783 12.75061 12.74096 12.72913 12.71541
## [449] 12.70005 12.68334 12.66555 12.64695 12.62781 12.60840 12.58900 12.56988
## [457] 12.55130 12.53354 12.51688 12.50158 12.48792 12.47616 12.46659 12.45947
## [465] 12.45290 12.44489 12.43558 12.42510 12.41359 12.40118 12.38800 12.37420
## [473] 12.35990 12.34524 12.33036 12.31539 12.30047 12.28573 12.27130 12.25732
## [481] 12.24394 12.23127 12.21946 12.20864 12.19894 12.19051 12.18347 12.17796
## [489] 12.17412 12.17208 12.17190 12.17346 12.17664 12.18132 12.18738 12.19470
## [497] 12.20315 12.21261 12.22297 12.23409 12.24586 12.25815 12.27085 12.28383
## [505] 12.29696 12.31014 12.32323 12.33611 12.34867 12.36078 12.37231 12.38316
## [513] 12.39318 12.40227 12.41030 12.41715 12.42270 12.42682 12.43080 12.43594
## [521] 12.44216 12.44937 12.45748 12.46642 12.47610 12.48642 12.49732 12.50870
## [529] 12.52048 12.53258 12.54490 12.55737 12.56990 12.58241 12.59480 12.60700
## [537] 12.61892 12.63048 12.64159 12.65217 12.66213 12.67139 12.67986 12.68746
## [545] 12.69411 12.69971 12.70418 12.70745 12.70942 12.71001 12.70914 12.70671
## [553] 12.70266 12.69671 12.68879 12.67907 12.66772 12.65491 12.64080 12.62557
## [561] 12.60939 12.59242 12.57484 12.55682 12.53853 12.52013 12.50180 12.48370
## [569] 12.46601 12.44890 12.43253 12.41708 12.40271 12.38960 12.37792 12.36783
## [577] 12.35745 12.34491 12.33039 12.31407 12.29612 12.27674 12.25609 12.23437
## [585] 12.21176 12.18842 12.16456 12.14033 12.11594 12.09155 12.06735 12.04353
## [593] 12.02025 11.99770 11.97607 11.95553 11.93626 11.91845 11.90227 11.88792
## [601] 11.87556 11.86538 11.85558 11.84437 11.83191 11.81836 11.80388 11.78866
## [609] 11.77284 11.75659 11.74008 11.72348 11.70694 11.69064 11.67473 11.65939
## [617] 11.64478 11.63106 11.61840 11.60696 11.59691 11.58841 11.58164 11.57674
## [625] 11.57390 11.57314 11.57433 11.57733 11.58202 11.58828 11.59598 11.60499
## [633] 11.61518 11.62644 11.63864 11.65165 11.66535 11.67960 11.69430 11.70930
## [641] 11.72449 11.73974 11.75492 11.76991 11.78459 11.79882 11.81248 11.82546
## [649] 11.83761 11.84882 11.85897 11.86941 11.88153 11.89521 11.91031 11.92672
## [657] 11.94432 11.96297 11.98257 12.00298 12.02408 12.04576 12.06787 12.09031
## [665] 12.11296 12.13568 12.15835 12.18085 12.20307 12.22487 12.24613 12.26673
## [673] 12.28654 12.30545 12.32333 12.34006 12.35551 12.36957 12.38210 12.39397
## [681] 12.40609 12.41843 12.43094 12.44358 12.45632 12.46910 12.48190 12.49466
## [689] 12.50736 12.51994 12.53237 12.54461 12.55662 12.56836 12.57978 12.59084
## [697] 12.60152 12.61175 12.62151 12.63076 12.63945 12.64754 12.65506 12.66207
## [705] 12.66861 12.67471 12.68039 12.68570 12.69065 12.69529 12.69963 12.70372
## [713] 12.70758 12.71124 12.71473 12.71809 12.72135 12.72452 12.72766 12.73078
## [721] 12.73391 12.73710 12.74036 12.74374 12.74725 12.75093 12.75481 12.75893
## [729] 12.76304 12.76691 12.77054 12.77394 12.77713 12.78011 12.78290 12.78550
## [737] 12.78793 12.79020 12.79232 12.79430 12.79615 12.79787 12.79949 12.80101
## [745] 12.80245 12.80380 12.80509 12.80633 12.80752 12.80867 12.80981 12.81080
## [753] 12.81153 12.81202 12.81229 12.81233 12.81218 12.81184 12.81133 12.81066
## [761] 12.80985 12.80891 12.80785 12.80670 12.80545 12.80414 12.80277 12.80135
## [769] 12.79990 12.79844 12.79698 12.79553 12.79411 12.79273 12.79140 12.79015
## [777] 12.78898 12.78772 12.78622 12.78449 12.78255 12.78041 12.77809 12.77561
## [785] 12.77299 12.77025 12.76739 12.76445 12.76143 12.75836 12.75525 12.75212
## [793] 12.74899 12.74587 12.74279 12.73976 12.73679 12.73391 12.73113 12.72847
## [801] 12.72595 12.72359 12.72140 12.71940 12.71760 12.71604 12.71456 12.71302
## [809] 12.71144 12.70981 12.70815 12.70645 12.70473 12.70300 12.70125 12.69949
## [817] 12.69773 12.69598 12.69425 12.69253 12.69083 12.68917 12.68754 12.68596
## [825] 12.68442 12.68295 12.68153 12.68018 12.67887 12.67755 12.67623 12.67492
## [833] 12.67360 12.67229 12.67098 12.66968 12.66839 12.66711 12.66583 12.66457
## [841] 12.66332 12.66209 12.66087 12.65967 12.65849 12.65733 12.65619 12.65507
## [849] 12.65398 12.65291 12.65187 12.65085 12.64987 12.64892 12.64802 12.64718
## [857] 12.64642 12.64571 12.64506 12.64446 12.64391 12.64341 12.64295 12.64253
## [865] 12.64215 12.64180 12.64147 12.64117 12.64090 12.64064 12.64039 12.64016
## [873] 12.63993 12.63970 12.63948
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 875)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.58199 12.57904 12.57615 12.57333 12.57057 12.56787 12.56524 12.56267
##   [9] 12.56016 12.55772 12.55534 12.55303 12.55078 12.54860 12.54649 12.54444
##  [17] 12.54246 12.54055 12.53871 12.53693 12.53523 12.53359 12.53202 12.53052
##  [25] 12.52910 12.52774 12.52646 12.52524 12.52410 12.52304 12.52204 12.52112
##  [33] 12.52027 12.51950 12.51880 12.51818 12.51763 12.51716 12.51676 12.51644
##  [41] 12.51620 12.51603 12.51595 12.51595 12.51605 12.51625 12.51655 12.51695
##  [49] 12.51744 12.51802 12.51870 12.51946 12.52032 12.52126 12.52229 12.52340
##  [57] 12.52460 12.52588 12.52724 12.52868 12.53019 12.53179 12.53346 12.53520
##  [65] 12.53701 12.53890 12.54085 12.54288 12.54497 12.54712 12.54934 12.55162
##  [73] 12.55396 12.55636 12.55882 12.56134 12.56391 12.56654 12.56922 12.57195
##  [81] 12.57473 12.57756 12.58043 12.58336 12.58632 12.58933 12.59238 12.59547
##  [89] 12.59860 12.60176 12.60496 12.60820 12.61162 12.61536 12.61940 12.62374
##  [97] 12.62836 12.63323 12.63836 12.64371 12.64929 12.65507 12.66103 12.66717
## [105] 12.67347 12.67992 12.68649 12.69318 12.69997 12.70684 12.71379 12.72079
## [113] 12.72783 12.73490 12.74198 12.74906 12.75612 12.76314 12.77012 12.77704
## [121] 12.78388 12.79063 12.79728 12.80380 12.81019 12.81643 12.82250 12.82839
## [129] 12.83409 12.83958 12.84484 12.84987 12.85464 12.85979 12.86588 12.87283
## [137] 12.88052 12.88887 12.89779 12.90716 12.91690 12.92691 12.93709 12.94734
## [145] 12.95758 12.96769 12.97759 12.98718 12.99636 13.00503 13.01310 13.02047
## [153] 13.02704 13.03273 13.03742 13.04102 13.04463 13.04936 13.05512 13.06183
## [161] 13.06940 13.07776 13.08681 13.09647 13.10666 13.11729 13.12828 13.13955
## [169] 13.15101 13.16258 13.17418 13.18571 13.19710 13.20827 13.21912 13.22957
## [177] 13.23955 13.24897 13.25773 13.26577 13.27299 13.27931 13.28465 13.28892
## [185] 13.29204 13.29392 13.29449 13.29365 13.29132 13.28725 13.28130 13.27360
## [193] 13.26427 13.25342 13.24118 13.22767 13.21300 13.19729 13.18068 13.16326
## [201] 13.14518 13.12654 13.10746 13.08808 13.06849 13.04884 13.02922 13.00978
## [209] 12.99062 12.97186 12.95363 12.93604 12.91922 12.90329 12.88836 12.87455
## [217] 12.86199 12.84886 12.83339 12.81581 12.79634 12.77517 12.75255 12.72866
## [225] 12.70375 12.67801 12.65166 12.62493 12.59802 12.57115 12.54454 12.51840
## [233] 12.49295 12.46840 12.44497 12.42288 12.40233 12.38355 12.36675 12.35215
## [241] 12.33891 12.32605 12.31355 12.30138 12.28954 12.27800 12.26673 12.25573
## [249] 12.24497 12.23443 12.22409 12.21394 12.20395 12.19411 12.18439 12.17478
## [257] 12.16525 12.15579 12.14638 12.13700 12.12762 12.11824 12.10883 12.09936
## [265] 12.08983 12.08021 12.07135 12.06401 12.05805 12.05335 12.04977 12.04718
## [273] 12.04543 12.04441 12.04396 12.04397 12.04429 12.04480 12.04535 12.04582
## [281] 12.04606 12.04595 12.04536 12.04414 12.04216 12.03929 12.03540 12.03035
## [289] 12.02401 12.01686 12.00949 12.00192 11.99417 11.98627 11.97824 11.97009
## [297] 11.96186 11.95357 11.94524 11.93688 11.92853 11.92021 11.91193 11.90373
## [305] 11.89561 11.88762 11.87976 11.87206 11.86455 11.85724 11.85016 11.84332
## [313] 11.83677 11.83051 11.82456 11.81763 11.80856 11.79755 11.78486 11.77069
## [321] 11.75527 11.73885 11.72163 11.70384 11.68573 11.66750 11.64939 11.63162
## [329] 11.61442 11.59802 11.58264 11.56851 11.55586 11.54492 11.53590 11.52904
## [337] 11.52457 11.52271 11.52229 11.52203 11.52194 11.52205 11.52237 11.52293
## [345] 11.52374 11.52484 11.52624 11.52797 11.53004 11.53248 11.53530 11.53853
## [353] 11.54220 11.54632 11.55091 11.55600 11.56160 11.56775 11.57445 11.58174
## [361] 11.58963 11.59814 11.60730 11.61713 11.62873 11.64306 11.65993 11.67914
## [369] 11.70049 11.72378 11.74883 11.77543 11.80339 11.83250 11.86258 11.89343
## [377] 11.92486 11.95665 11.98863 12.02059 12.05234 12.08367 12.11440 12.14433
## [385] 12.17326 12.20099 12.22733 12.25208 12.27505 12.29604 12.31485 12.33129
## [393] 12.34762 12.36612 12.38652 12.40862 12.43217 12.45693 12.48269 12.50919
## [401] 12.53622 12.56353 12.59090 12.61808 12.64485 12.67098 12.69623 12.72036
## [409] 12.74315 12.76435 12.78375 12.80110 12.81617 12.82873 12.83854 12.84632
## [417] 12.85296 12.85851 12.86304 12.86661 12.86927 12.87108 12.87210 12.87238
## [425] 12.87198 12.87097 12.86940 12.86732 12.86480 12.86188 12.85864 12.85513
## [433] 12.85140 12.84751 12.84353 12.83950 12.83549 12.83156 12.82775 12.82414
## [441] 12.82077 12.81593 12.80806 12.79744 12.78434 12.76904 12.75182 12.73295
## [449] 12.71273 12.69142 12.66931 12.64668 12.62379 12.60094 12.57839 12.55644
## [457] 12.53536 12.51542 12.49690 12.48009 12.46526 12.45270 12.44267 12.43547
## [465] 12.42897 12.42097 12.41161 12.40104 12.38941 12.37685 12.36351 12.34953
## [473] 12.33505 12.32023 12.30519 12.29010 12.27508 12.26028 12.24586 12.23194
## [481] 12.21867 12.20621 12.19468 12.18424 12.17502 12.16718 12.16085 12.15618
## [489] 12.15331 12.15239 12.15328 12.15570 12.15955 12.16472 12.17112 12.17864
## [497] 12.18718 12.19665 12.20694 12.21795 12.22957 12.24172 12.25429 12.26717
## [505] 12.28027 12.29348 12.30671 12.31985 12.33281 12.34548 12.35776 12.36955
## [513] 12.38074 12.39125 12.40097 12.40979 12.41762 12.42435 12.43159 12.44090
## [521] 12.45214 12.46517 12.47986 12.49607 12.51364 12.53246 12.55236 12.57322
## [529] 12.59490 12.61725 12.64014 12.66343 12.68697 12.71062 12.73426 12.75773
## [537] 12.78090 12.80363 12.82577 12.84720 12.86776 12.88732 12.90574 12.92288
## [545] 12.93860 12.95276 12.96522 12.97584 12.98448 12.99100 12.99527 12.99713
## [553] 12.99646 12.99342 12.98836 12.98146 12.97286 12.96271 12.95119 12.93844
## [561] 12.92461 12.90987 12.89437 12.87827 12.86172 12.84488 12.82790 12.81095
## [569] 12.79417 12.77772 12.76177 12.74645 12.73194 12.71839 12.70595 12.69478
## [577] 12.68268 12.66749 12.64947 12.62886 12.60592 12.58091 12.55405 12.52562
## [585] 12.49585 12.46501 12.43333 12.40106 12.36847 12.33579 12.30328 12.27119
## [593] 12.23976 12.20925 12.17991 12.15199 12.12574 12.10140 12.07924 12.05948
## [601] 12.04240 12.02824 12.01452 11.99875 11.98117 11.96200 11.94146 11.91978
## [609] 11.89720 11.87392 11.85019 11.82623 11.80226 11.77851 11.75521 11.73258
## [617] 11.71086 11.69026 11.67101 11.65335 11.63749 11.62366 11.61210 11.60301
## [625] 11.59665 11.59256 11.59013 11.58924 11.58982 11.59176 11.59498 11.59937
## [633] 11.60486 11.61133 11.61871 11.62689 11.63579 11.64530 11.65535 11.66583
## [641] 11.67665 11.68772 11.69894 11.71023 11.72148 11.73261 11.74352 11.75412
## [649] 11.76431 11.77401 11.78311 11.79307 11.80527 11.81956 11.83578 11.85378
## [657] 11.87339 11.89446 11.91683 11.94035 11.96485 11.99018 12.01619 12.04271
## [665] 12.06959 12.09667 12.12379 12.15080 12.17754 12.20384 12.22956 12.25454
## [673] 12.27862 12.30163 12.32343 12.34386 12.36276 12.37997 12.39533 12.41009
## [681] 12.42554 12.44160 12.45820 12.47525 12.49269 12.51043 12.52840 12.54651
## [689] 12.56470 12.58289 12.60100 12.61895 12.63666 12.65406 12.67107 12.68762
## [697] 12.70363 12.71901 12.73370 12.74761 12.76067 12.77281 12.78427 12.79538
## [705] 12.80614 12.81658 12.82670 12.83652 12.84606 12.85532 12.86433 12.87309
## [713] 12.88162 12.88993 12.89804 12.90596 12.91370 12.92129 12.92872 12.93602
## [721] 12.94320 12.95028 12.95726 12.96416 12.97100 12.97779 12.98454 12.99127
## [729] 12.99799 13.00471 13.01141 13.01806 13.02465 13.03115 13.03755 13.04383
## [737] 13.04997 13.05595 13.06174 13.06733 13.07271 13.07784 13.08271 13.08731
## [745] 13.09161 13.09558 13.09922 13.10251 13.10541 13.10792 13.11001 13.11165
## [753] 13.11283 13.11358 13.11393 13.11389 13.11349 13.11276 13.11172 13.11039
## [761] 13.10881 13.10699 13.10497 13.10275 13.10038 13.09787 13.09526 13.09255
## [769] 13.08979 13.08699 13.08418 13.08138 13.07862 13.07592 13.07331 13.07081
## [777] 13.06845 13.06591 13.06286 13.05936 13.05542 13.05109 13.04640 13.04139
## [785] 13.03610 13.03056 13.02480 13.01886 13.01278 13.00659 13.00033 12.99403
## [793] 12.98773 12.98147 12.97527 12.96919 12.96324 12.95747 12.95192 12.94661
## [801] 12.94159 12.93688 12.93254 12.92858 12.92505 12.92198 12.91908 12.91605
## [809] 12.91289 12.90963 12.90627 12.90283 12.89933 12.89577 12.89218 12.88856
## [817] 12.88493 12.88130 12.87770 12.87412 12.87060 12.86713 12.86374 12.86043
## [825] 12.85723 12.85415 12.85119 12.84839 12.84564 12.84287 12.84009 12.83729
## [833] 12.83447 12.83165 12.82883 12.82601 12.82319 12.82038 12.81759 12.81481
## [841] 12.81206 12.80933 12.80663 12.80397 12.80134 12.79876 12.79622 12.79373
## [849] 12.79130 12.78892 12.78661 12.78437 12.78220 12.78010 12.77811 12.77627
## [857] 12.77455 12.77297 12.77150 12.77014 12.76888 12.76771 12.76663 12.76563
## [865] 12.76470 12.76383 12.76302 12.76225 12.76152 12.76082 12.76015 12.75949
## [873] 12.75884 12.75818 12.75752
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 875)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.03019 12.02422 12.01833 12.01253 12.00682 12.00120 11.99567 11.99022
##   [9] 11.98487 11.97961 11.97443 11.96934 11.96434 11.95943 11.95461 11.94988
##  [17] 11.94523 11.94068 11.93621 11.93183 11.92754 11.92334 11.91922 11.91519
##  [25] 11.91125 11.90740 11.90364 11.89996 11.89638 11.89288 11.88946 11.88614
##  [33] 11.88290 11.87975 11.87669 11.87371 11.87083 11.86803 11.86531 11.86269
##  [41] 11.86015 11.85770 11.85533 11.85305 11.85086 11.84876 11.84674 11.84481
##  [49] 11.84296 11.84120 11.83956 11.83804 11.83666 11.83541 11.83428 11.83329
##  [57] 11.83241 11.83166 11.83103 11.83052 11.83012 11.82984 11.82968 11.82962
##  [65] 11.82968 11.82984 11.83011 11.83048 11.83095 11.83153 11.83220 11.83298
##  [73] 11.83384 11.83480 11.83585 11.83699 11.83822 11.83953 11.84093 11.84241
##  [81] 11.84397 11.84561 11.84733 11.84912 11.85098 11.85292 11.85493 11.85700
##  [89] 11.85914 11.86134 11.86360 11.86593 11.86831 11.87075 11.87324 11.87579
##  [97] 11.87839 11.88104 11.88373 11.88652 11.88945 11.89251 11.89571 11.89903
## [105] 11.90249 11.90608 11.90979 11.91363 11.91759 11.92167 11.92587 11.93018
## [113] 11.93461 11.93915 11.94381 11.94857 11.95344 11.95842 11.96349 11.96868
## [121] 11.97395 11.97933 11.98480 11.99037 11.99603 12.00178 12.00761 12.01354
## [129] 12.01954 12.02563 12.03180 12.03805 12.04437 12.05077 12.05724 12.06491
## [137] 12.07479 12.08669 12.10045 12.11588 12.13281 12.15108 12.17049 12.19087
## [145] 12.21205 12.23386 12.25611 12.27863 12.30125 12.32378 12.34606 12.36790
## [153] 12.38913 12.40958 12.42907 12.44742 12.46446 12.48000 12.49389 12.50593
## [161] 12.51596 12.52533 12.53550 12.54638 12.55792 12.57004 12.58268 12.59576
## [169] 12.60923 12.62300 12.63701 12.65120 12.66549 12.67982 12.69412 12.70831
## [177] 12.72234 12.73613 12.74961 12.76272 12.77539 12.78755 12.79912 12.81005
## [185] 12.82027 12.82970 12.83828 12.84593 12.85260 12.85820 12.86269 12.86597
## [193] 12.86800 12.86869 12.86798 12.86580 12.86145 12.85442 12.84491 12.83316
## [201] 12.81938 12.80378 12.78659 12.76802 12.74829 12.72762 12.70623 12.68433
## [209] 12.66215 12.63990 12.61779 12.59606 12.57491 12.55456 12.53524 12.51716
## [217] 12.50053 12.48558 12.47253 12.45945 12.44439 12.42749 12.40893 12.38884
## [225] 12.36739 12.34474 12.32103 12.29642 12.27107 12.24513 12.21876 12.19211
## [233] 12.16534 12.13860 12.11205 12.08584 12.06013 12.03508 12.01083 11.98755
## [241] 11.96539 11.94450 11.92504 11.90716 11.89103 11.87592 11.86101 11.84629
## [249] 11.83175 11.81739 11.80319 11.78914 11.77524 11.76148 11.74784 11.73432
## [257] 11.72091 11.70760 11.69439 11.68125 11.66819 11.65519 11.64224 11.62934
## [265] 11.61648 11.60364 11.59083 11.57802 11.56551 11.55358 11.54218 11.53131
## [273] 11.52091 11.51096 11.50143 11.49229 11.48351 11.47505 11.46688 11.45898
## [281] 11.45131 11.44384 11.43654 11.42937 11.42232 11.41534 11.40840 11.40147
## [289] 11.39453 11.38754 11.38047 11.37328 11.36596 11.35846 11.35080 11.34304
## [297] 11.33521 11.32733 11.31944 11.31157 11.30373 11.29597 11.28831 11.28079
## [305] 11.27342 11.26623 11.25927 11.25255 11.24611 11.23996 11.23416 11.22871
## [313] 11.22365 11.21901 11.21482 11.21111 11.20790 11.20423 11.19920 11.19295
## [321] 11.18561 11.17733 11.16823 11.15845 11.14813 11.13741 11.12642 11.11530
## [329] 11.10418 11.09320 11.08250 11.07221 11.06247 11.05342 11.04518 11.03791
## [337] 11.03173 11.02678 11.02320 11.02112 11.02068 11.02202 11.02527 11.03026
## [345] 11.03666 11.04438 11.05332 11.06338 11.07448 11.08651 11.09938 11.11300
## [353] 11.12727 11.14209 11.15737 11.17301 11.18892 11.20501 11.22117 11.23732
## [361] 11.25335 11.26918 11.28470 11.29982 11.31445 11.32849 11.34354 11.36114
## [369] 11.38110 11.40323 11.42732 11.45320 11.48065 11.50951 11.53956 11.57062
## [377] 11.60249 11.63498 11.66790 11.70105 11.73425 11.76729 11.79999 11.83215
## [385] 11.86359 11.89410 11.92349 11.95157 11.97815 12.00304 12.02604 12.04695
## [393] 12.06559 12.08177 12.09695 12.11270 12.12894 12.14559 12.16257 12.17982
## [401] 12.19726 12.21482 12.23241 12.24996 12.26741 12.28467 12.30166 12.31833
## [409] 12.33458 12.35034 12.36554 12.38011 12.39396 12.40703 12.41924 12.43052
## [417] 12.44078 12.44996 12.45797 12.46475 12.47040 12.47512 12.47895 12.48195
## [425] 12.48418 12.48569 12.48654 12.48678 12.48647 12.48566 12.48441 12.48277
## [433] 12.48080 12.47855 12.47608 12.47344 12.47069 12.46789 12.46508 12.46232
## [441] 12.45967 12.45718 12.45491 12.45159 12.44605 12.43852 12.42919 12.41830
## [449] 12.40604 12.39264 12.37830 12.36325 12.34768 12.33183 12.31589 12.30009
## [457] 12.28464 12.26975 12.25563 12.24250 12.23057 12.22005 12.21117 12.20412
## [465] 12.19707 12.18815 12.17753 12.16537 12.15185 12.13713 12.12138 12.10478
## [473] 12.08749 12.06968 12.05152 12.03317 12.01482 11.99663 11.97876 11.96138
## [481] 11.94468 11.92880 11.91394 11.90024 11.88789 11.87705 11.86789 11.86058
## [489] 11.85529 11.85219 11.85074 11.85027 11.85071 11.85201 11.85412 11.85697
## [497] 11.86052 11.86470 11.86945 11.87472 11.88046 11.88661 11.89310 11.89989
## [505] 11.90692 11.91413 11.92146 11.92886 11.93627 11.94364 11.95090 11.95800
## [513] 11.96489 11.97151 11.97780 11.98370 11.98916 11.99413 11.99962 12.00664
## [521] 12.01509 12.02488 12.03589 12.04804 12.06122 12.07534 12.09030 12.10599
## [529] 12.12233 12.13920 12.15652 12.17418 12.19208 12.21013 12.22823 12.24627
## [537] 12.26416 12.28180 12.29910 12.31594 12.33224 12.34790 12.36281 12.37687
## [545] 12.39000 12.40208 12.41303 12.42273 12.43110 12.43804 12.44344 12.44720
## [553] 12.44924 12.45012 12.45052 12.45046 12.44997 12.44907 12.44779 12.44616
## [561] 12.44420 12.44194 12.43940 12.43661 12.43360 12.43039 12.42701 12.42349
## [569] 12.41984 12.41610 12.41230 12.40845 12.40459 12.40073 12.39692 12.39316
## [577] 12.38842 12.38170 12.37317 12.36297 12.35127 12.33821 12.32394 12.30863
## [585] 12.29242 12.27546 12.25792 12.23993 12.22167 12.20327 12.18489 12.16668
## [593] 12.14881 12.13141 12.11465 12.09867 12.08363 12.06969 12.05699 12.04569
## [601] 12.03595 12.02790 12.01966 12.00933 11.99714 11.98330 11.96801 11.95150
## [609] 11.93397 11.91564 11.89672 11.87742 11.85795 11.83853 11.81936 11.80067
## [617] 11.78266 11.76554 11.74953 11.73484 11.72168 11.71027 11.70081 11.69352
## [625] 11.68861 11.68569 11.68416 11.68394 11.68493 11.68706 11.69023 11.69436
## [633] 11.69936 11.70514 11.71162 11.71872 11.72633 11.73438 11.74278 11.75144
## [641] 11.76028 11.76920 11.77813 11.78697 11.79564 11.80405 11.81212 11.81975
## [649] 11.82686 11.83336 11.83917 11.84534 11.85294 11.86185 11.87197 11.88320
## [657] 11.89544 11.90857 11.92250 11.93712 11.95234 11.96803 11.98411 12.00047
## [665] 12.01700 12.03360 12.05017 12.06660 12.08279 12.09863 12.11403 12.12887
## [673] 12.14306 12.15649 12.16906 12.18066 12.19119 12.20054 12.20862 12.21609
## [681] 12.22368 12.23137 12.23914 12.24697 12.25484 12.26273 12.27063 12.27850
## [689] 12.28634 12.29412 12.30182 12.30943 12.31692 12.32427 12.33146 12.33848
## [697] 12.34530 12.35191 12.35827 12.36439 12.37023 12.37577 12.38092 12.38563
## [705] 12.38992 12.39382 12.39736 12.40057 12.40349 12.40613 12.40854 12.41073
## [713] 12.41275 12.41461 12.41636 12.41801 12.41960 12.42115 12.42271 12.42429
## [721] 12.42593 12.42765 12.42949 12.43148 12.43364 12.43601 12.43861 12.44148
## [729] 12.44449 12.44749 12.45048 12.45346 12.45641 12.45933 12.46221 12.46504
## [737] 12.46781 12.47053 12.47318 12.47576 12.47825 12.48065 12.48296 12.48517
## [745] 12.48726 12.48924 12.49110 12.49282 12.49440 12.49584 12.49713 12.49831
## [753] 12.49945 12.50055 12.50159 12.50259 12.50353 12.50443 12.50527 12.50607
## [761] 12.50680 12.50749 12.50811 12.50868 12.50920 12.50965 12.51004 12.51038
## [769] 12.51065 12.51086 12.51100 12.51108 12.51110 12.51104 12.51092 12.51073
## [777] 12.51048 12.51011 12.50959 12.50894 12.50815 12.50725 12.50624 12.50512
## [785] 12.50391 12.50261 12.50123 12.49979 12.49828 12.49672 12.49512 12.49348
## [793] 12.49181 12.49012 12.48843 12.48673 12.48504 12.48336 12.48171 12.48009
## [801] 12.47851 12.47697 12.47550 12.47409 12.47275 12.47150 12.47026 12.46896
## [809] 12.46761 12.46620 12.46473 12.46322 12.46166 12.46006 12.45842 12.45675
## [817] 12.45503 12.45329 12.45152 12.44972 12.44790 12.44606 12.44420 12.44233
## [825] 12.44044 12.43855 12.43665 12.43475 12.43283 12.43086 12.42886 12.42681
## [833] 12.42472 12.42259 12.42043 12.41824 12.41602 12.41376 12.41147 12.40916
## [841] 12.40683 12.40446 12.40208 12.39968 12.39725 12.39481 12.39236 12.38989
## [849] 12.38741 12.38491 12.38241 12.37990 12.37739 12.37487 12.37236 12.36987
## [857] 12.36740 12.36494 12.36250 12.36006 12.35763 12.35521 12.35278 12.35035
## [865] 12.34792 12.34547 12.34302 12.34055 12.33806 12.33555 12.33302 12.33046
## [873] 12.32787 12.32525 12.32260
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")